﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class MasterPages_MasterPage : System.Web.UI.MasterPage
{
    protected String[] Message
    {
        get
        {
            if (Session["Message"] == null)
            {
                String[] m = { "", "" };
                return m;
            }
            return (String[])Session["Message"];
        }
        set { Session["Message"] = value; }
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Message[0] == "")
        {
            ltrShowMessage.Text = "0";
        }
        else
        {
            ltrShowMessage.Text = "1";
            ltrMessage.Text = Message[1];
            ltrMessageType.Text = Message[0]; // error, warning, information
        }
        String[] msg = { "", "" };
        Message = msg;
    }
    protected void lnkLogout_Click(object sender, EventArgs e)
    {
        Util.DeleteCookies(GlobalNames.userCookieInfo);
        this.Response.Redirect("~/Login.aspx");

    }
    protected void Page_Init(object sender, EventArgs e)
    {
        bool products = false;
        bool users = false;
        bool fabrics = false;
        bool sizes = false;
        bool models = false;

        string url = this.Page.Request.Url.ToString().ToLower();
        if (!url.ToLower().Contains("login.aspx"))
        {
            if (!Util.ExistUserCookie())
            {
                Response.Redirect("~/Login.aspx?redirectUrl=" + Server.UrlEncode(this.Page.Request.Url.ToString()));
            }
            if (Session[GlobalNames.UserPermissions] == null || (Session[GlobalNames.UserPermissions] != null && ((List<SessionPermissions>)Session[GlobalNames.UserPermissions]).Count == 0))
            {
                Response.Redirect("~/Login.aspx?redirectUrl=" + Server.UrlEncode(this.Page.Request.Url.ToString()));
            }
            else
            {
                List<SessionPermissions> permissions = (List<SessionPermissions>)Session[GlobalNames.UserPermissions];
                foreach (SessionPermissions s in permissions)
                {
                    
                    if ((s.ModuleId == (long)GlobalEnums.Modules.Productos || s.ModuleId == (long)GlobalEnums.Modules.Usuarios
                        || s.ModuleId == (long)GlobalEnums.Modules.Telas
                        || s.ModuleId == (long)GlobalEnums.Modules.Tallas
                        || s.ModuleId == (long)GlobalEnums.Modules.Modelos
                        ) && s.PermissionId 
                        == (long)GlobalEnums.Permissions.Ver)
                    {
                        if (s.ModuleId == (long)GlobalEnums.Modules.Productos)
                            products = true;
                        if (s.ModuleId == (long)GlobalEnums.Modules.Usuarios)
                            users = true;
                        if (s.ModuleId == (long)GlobalEnums.Modules.Telas)
                            fabrics = true;
                        if (s.ModuleId == (long)GlobalEnums.Modules.Tallas)
                            sizes = true;
                        if (s.ModuleId == (long)GlobalEnums.Modules.Modelos)
                            models = true;
                        liConfiguration.Visible = true;
                    }
                    else if (s.ModuleId == (long)GlobalEnums.Modules.Procesos
                        && s.PermissionId == (long)GlobalEnums.Permissions.Ver)
                    {
                        liProcesses.Visible = true;
                    }
                    else if (s.ModuleId == (long)GlobalEnums.Modules.Cortes
                        && s.PermissionId == (long)GlobalEnums.Permissions.Ver)
                    {
                        liProductions.Visible = true;
                    }
                    else if (s.ModuleId == (long)GlobalEnums.Modules.Lavanderia
                        && s.PermissionId == (long)GlobalEnums.Permissions.Ver)
                    {
                        liLaundry.Visible = true;
                    }
                    else if ((s.ModuleId == (long)GlobalEnums.Modules.Reporte_Ventas || s.ModuleId == (long)GlobalEnums.Modules.Reporte_Productos)
                        && s.PermissionId == (long)GlobalEnums.Permissions.Ver)
                    {
                        liReports.Visible = true;
                    }
                    if (liProcesses.Visible == true && liConfiguration.Visible == true 
                        && liProductions.Visible == true && liLaundry.Visible == true && liReports.Visible == true)
                    {
                        break;
                    }
                }
                hypConfiguration.NavigateUrl = (products) ? "~/Configuracion/Productos/Default.aspx" :
                    (users) ? "~/Configuracion/Usuarios/Default.aspx" : (fabrics) ? "~/Configuracion/Telas/Default.aspx"
                    : (sizes) ? "~/Configuracion/Tallas/Default.aspx" : models ? "~/Configuracion/Modelos/Default.aspx" : "";
                if (url.Contains("/Configuracion/".ToLower()))
                {
                    liConfiguration.Attributes.Add("class", "selected");
                }
                else if (url.Contains("/Procesos/".ToLower()))
                {
                    liProcesses.Attributes.Add("class", "selected");
                }
                else if (url.Contains("/Cortes/".ToLower()))
                {
                    liProductions.Attributes.Add("class", "selected");
                }
                else if (url.Contains("/Lavanderia/".ToLower()))
                {
                    liLaundry.Attributes.Add("class", "selected");
                }
                else if (url.Contains("/Reportes/".ToLower()))
                {
                    liReports.Attributes.Add("class", "selected");
                }
            }
        }
        else
        {
            pageContainer.Attributes.Add("style", "background:none");
        }
    }
}
